import threading
import time
import os
import platform
import subprocess
import json

import toml

from logger.logger import Log


def get_time() -> str:
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

def get_time_time() -> time:
    print(time.localtime().tm_hour)
    print(time.localtime().tm_min)
    print(time.localtime().tm_mon)
    print(time.localtime().tm_sec)
    print(time.localtime().tm_mday)
    print(time.localtime().tm_wday)
    print(time.localtime().tm_isdst)

    return time.localtime()


def timestr_to_timestamp(timestr):
    timestr1, timestr2 = timestr.split('.')
    struct_time = time.strptime(timestr1, '%Y-%m-%d %H:%M:%S')
    seconds = time.mktime(struct_time)
    millseconds = float("0." + timestr2)
    return seconds + millseconds


def excel_set_value_with_args(sheet, row, column, value, font, align, fill):
    cell = sheet.cell(row, column, value)
    if font is not None:
        cell.font = font
    if align is not None:
        cell.alignment = align
    if fill is not None:
        cell.fill = fill


def open_file(path):
    if platform.system() == "Windows":
        os.startfile(path)
    elif platform.system() == "Darwin":
        subprocess.Popen(["open", path])
    else:
        subprocess.Popen(["xdg-open", path])


def run_with_thread(call):
    t = threading.Thread(target=call)
    t.start()


def export_jsonfile(data: dict, filename):
    try:
        with open(filename, "w") as f:
            json.dump(data, f, indent=1)
    except Exception as e:
        Log.logger.error('export json file error,', e)
        return

class PreserveInlineEncoder(toml.TomlEncoder):
    def dump_inline_dict(self, dct):
        if dct:
            return super().dump_inline_dict(dct) + ","
        else:
            return super().dump_inline_dict(dct)
